using CacheManager.Core;
using NPOI.SS.Formula.Functions;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common.Cache
{
    /// <summary>
    /// 定义缓存服务接口
    /// 提供基本的缓存操作功能，支持本地内存缓存和分布式缓存
    /// </summary>
    public interface ICacheService
    {
        /// <summary>
        /// 从缓存中获取指定键的值
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns>如果找到则返回对应的值，否则返回default(T)</returns>
        T Get<T>(string key);
        Task<T> GetAsync<T>(string key);

        /// <summary>
        /// 设置缓存值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="value">要缓存的值</param>
        /// <param name="expiry">过期时间，null表示使用默认过期时间</param>
        void Set<T>(string key, T value, TimeSpan? expiry = null);
        Task SetAsync<T>(string key, T value, TimeSpan? expiry = null);

        T GetOrAdd<T>(string key, Func<T> factory, TimeSpan? timeSpan = null);
        Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> factory, TimeSpan? timeSpan = null);
        /// <summary>
        /// 检查指定键是否存在于缓存中
        /// </summary>
        /// <param name="key">要检查的缓存键</param>
        /// <returns>如果键存在返回true，否则返回false</returns>
        bool Exists(string key);
        Task<bool> ExistsAsync(string key);
        /// <summary>
        /// 从缓存中移除指定键
        /// </summary>
        /// <param name="key">要移除的缓存键</param>
        void Remove(string key);
        Task RemoveAsync(string key);
        /// <summary>
        /// 获取Redis数据库实例(当使用Redis时)
        /// </summary>
        IDatabase RedisDatabase { get; }
        /// <summary>
        /// 获取Redis连接实例(当使用Redis时)
        /// </summary>
        IConnectionMultiplexer ConnMultiplexer { get; }
        /// <summary>
        /// 获取是否启用了分布式缓存
        /// </summary>
        bool IsDistributedCacheEnabled { get; }
    }
}
